package com.lyy.controller;

import cn.hutool.core.bean.BeanUtil;
import com.lyy.annotation.SkipToken;
import com.lyy.entity.User;
import com.lyy.entity.param.*;

import com.lyy.result.Result;
import com.lyy.result.ResultCode;
import com.lyy.service.UserService;
import com.lyy.util.JwtUtil;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpSession;
import java.util.Map;

/**
 * @author Liangyaoyun
 */
//@RestController=@Controller+@ResponseBody(返回json数据)
 @Tag(name = "User接口")
@RestController
@RequestMapping("/api/v1/user")
public class UserController {
    @Autowired
    private UserService userService;
    @Operation(summary = "注册新的用户")
    @PostMapping("/register")
    public Result register(@Validated @RequestBody UserRegisterParam param) {
        User user = BeanUtil.copyProperties(param, User.class);
        User result = userService.register(user);
        return new Result(result);
    }

    @Operation(summary = "账号密码登录")
    @PostMapping("/loginByAccount")
    public Result loginByAccount(@Validated @RequestBody UserLoginByAccountParam param) {
        User user = userService.loginByAccount(param.getUsername(), param.getPassword());
        // 根据用户部分信息生成一个Token字符串
        String token = JwtUtil.build(user.getUserId(), user.getNickName(), user.getAvatarUrl());
        // 不建议在这里将用户ID存储在Session中，建议在Token拦截器中储存
        return new Result(Map.of("user", user, "token", token));
    }

    @Operation(summary = "查询个人信息")
    @GetMapping("/selectPersonal")
    public Result selectPersonal(HttpSession session) {
        Integer userId = (Integer) session.getAttribute("userId");
        User user = userService.selectByUserId(userId);
        return new Result(user);
    }

    @Operation(summary = "修改个人信息")
    @PostMapping("/updatePersonal")
    public Result updatePersonal(@Validated @RequestBody UserUpdateParam param, HttpSession session) {
        Integer userId = (Integer) session.getAttribute("userId");
        User user = BeanUtil.copyProperties(param, User.class);
        userService.updateByUserId(user, userId);
        return new Result(ResultCode.SUCCESS);
    }

    @Operation(summary = "修改个人密码")
    @PostMapping("/updatePassword")
    public Result updatePassword(@Validated @RequestBody UserUpdatePasswordParam param, HttpSession session) {
        Integer userId = (Integer) session.getAttribute("userId");
        userService.updatePassword(userId, param.getOldPassword(), param.getNewPassword());
        return new Result(ResultCode.SUCCESS);
    }

    @Operation(summary = "注销个人账号")
    @PostMapping("/deletePersonal")
    public Result deletePersonal(HttpSession session) {
        Integer userId = (Integer) session.getAttribute("userId");
        userService.deleteByUserId(userId);
        return new Result(ResultCode.SUCCESS);
    }

    /**
     * 这里不要使用 @RequestParam 因为文件类型不是 application/json
     */
    @Operation(summary = "上传个人头像")
    @PostMapping("/uploadAvatar")
    public Result uploadAvatar(MultipartFile avatarFile, HttpSession session) {
        Integer userId = (Integer) session.getAttribute("userId");
        userService.uploadAvatar(avatarFile, userId);
        return new Result(ResultCode.SUCCESS);
    }
    @Operation(summary = "生成验证码")
    @GetMapping("/generateVcode")
    public Result generateVcode( @RequestParam String phone) {
     String vCode=userService.generateVcode(phone);
     return new Result(vCode);
    }
    @Operation(summary = "按照手机号登录")
    @PostMapping("/loginByPhone")
    public Result loginByPhone(@Validated @RequestBody UserLoginByPhoneParam userLoginByPhoneParam) {
        User user=userService.loginByPhone(userLoginByPhoneParam.getVcode(),userLoginByPhoneParam.getPhone());
        String token=JwtUtil.build(user.getUserId(),user.getNickName(),user.getAvatarUrl());
        return new Result(Map.of("user",user,"token",token));
    }
    @Operation(summary = "查询用户积分")
    @GetMapping("/selectPoints")
    public Result selectPoints(HttpSession httpSession) {
        Integer userId = (Integer) httpSession.getAttribute("userId");
        Double result = userService.selectPoints(userId);
        return new Result(result);
    }
}
